package com.tanhua.server.interceptor;

import com.tanhua.domain.db.User;

/**
 * 职责：处理把认证后的用户对象绑定到线程对象上、以及从当前线程对象上获取登陆用户
 */
public class UserHolder {
    // 创建本地线程对象
    private static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    //1. 处理把认证后的用户对象绑定到线程对象上
    public static void set(User user){
        threadLocal.set(user);
    }

    //2. 从当前线程对象上获取登陆用户
    public static User get(){
        return threadLocal.get();
    }

    //3. 获取用户id
    public static Long getUserId(){
        return get().getId();
    }
}
